package com.xuequ.answerme.manager;

import android.content.Context;

import com.xuequ.answerme.beans.AnswerBean;
import com.xuequ.answerme.beans.QuestionBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.tools.Tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobDate;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.listener.CountListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Shijie on 2015/10/2.
 */
public class AnswerManager {

    //回答问题
    public interface AnswerQuestionListener {
        void answerQuestionOnSuccess();// 回答问题成功

        void answerQuestionOnFailure(String failureMsg);// 回答问题失败返回失败消息
    }

    //查找对应问题的答案
    public interface FindAllAnswersListener {
        void findAllAnswersOnSuccess(List<AnswerBean> answers);// 查找答案成功返回Answers列表

        void findAllAnswersOnFailure(String failureMsg);// 查找失败返回失败消息
    }

    //修改答案
    public interface ChangeAnswerListener {
        void ChangeAnswerOnSuccess();// 修改答案成功

        void ChangeAnswerOnFailure(String failureMsg);// 修改答案失败返回失败信息
    }

    //修改获赞数

    //
    public interface FindMostLikesAnswerListener {
        void findMostLikesAnswerOnSuccess(AnswerBean answerBean);

        void findMostLikesAnswerOnFailure(String failureMsg);
    }

    public interface FindAllMyAnswersListener {
        void findMyAnswersOnSuccess(List<AnswerBean> answers, int count);

        void findMyAnswersOnFailure(String failureMsg);
    }

    public interface LikeNumberChangeListener {
        void likeNumberChangeOnSuccess();

        void likeNumberChangeOnFailure(String failureMsg);
    }

    public interface CheckTheAnswerListener {
        void checkTheAnswerOnSuccess(AnswerBean answerBean);

        void checkTheAnswerOnFailure(String failureMsg);
    }

    public interface LoadMoreAnswersListener {
        void loadMoreAnswersOnSuccess(List<AnswerBean> answerBeans);

        void loadMoreAnswersOnFailure(String failureMsg);
    }

    /**
     * 回答问题
     * （在上传之前注意绑定回答人ID和回答问题ID）
     *
     * @param context                上下文
     * @param answerBean             回答bean
     * @param answerQuestionListener 回答listener
     */
    public void answerQuestion(Context context, AnswerBean answerBean, final AnswerQuestionListener answerQuestionListener) {
        if (Tools.isNetAvailable(context)) {
            answerBean.save(context, new SaveListener() {
                @Override
                public void onSuccess() {
                    answerQuestionListener.answerQuestionOnSuccess();
                }

                @Override
                public void onFailure(int i, String s) {
                    answerQuestionListener.answerQuestionOnFailure(s);
                }
            });
        } else {
            answerQuestionListener.answerQuestionOnFailure("网络不可用");
        }

    }

    /**
     * 寻找某问题的所有答案
     * （当打开某问题的时候，自动搜索问题答案）
     *
     * @param context                上下文
     * @param answer_of_question     目标问题
     * @param findAllAnswersListener 监听器
     */
    public void findAllAnswersForAll(Context context, QuestionBean answer_of_question, int position, final FindAllAnswersListener findAllAnswersListener) {

        if (Tools.isNetAvailable(context)) {
            BmobQuery<AnswerBean> answers = new BmobQuery<>();
            //寻找有相同问题id的答案
            answers.addWhereEqualTo("answer_of_question", new BmobPointer(answer_of_question));
            //给答案排序
            //根据赞的降序来显示
            answers.setLimit(20);
            answers.include("answerer,answer_of_question");
            answers.findObjects(context, new FindListener<AnswerBean>() {
                @Override
                public void onSuccess(List<AnswerBean> list) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setAnswer_objectId(list.get(i).getObjectId());
                    }
                    findAllAnswersListener.findAllAnswersOnSuccess(list);
                }

                @Override
                public void onError(int i, String s) {
                    findAllAnswersListener.findAllAnswersOnFailure(s);
                }
            });
        } else {
            findAllAnswersListener.findAllAnswersOnFailure("网络不可用");
        }
    }


    public void findAllAnswers(Context context, QuestionBean answer_of_question, final FindAllAnswersListener findAllAnswersListener) {

        if (Tools.isNetAvailable(context)) {
            BmobQuery<AnswerBean> answers = new BmobQuery<>();
            //寻找有相同问题id的答案
            answers.addWhereEqualTo("answer_of_question", new BmobPointer(answer_of_question));
            //给答案排序
            //根据赞的降序来显示
            answers.setLimit(10);
            answers.include("answerer,answer_of_question");
            answers.findObjects(context, new FindListener<AnswerBean>() {
                @Override
                public void onSuccess(List<AnswerBean> list) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setAnswer_objectId(list.get(i).getObjectId());
                    }
                    findAllAnswersListener.findAllAnswersOnSuccess(list);
                }

                @Override
                public void onError(int i, String s) {
                    findAllAnswersListener.findAllAnswersOnFailure(s);
                }
            });
        } else {
            findAllAnswersListener.findAllAnswersOnFailure("网络不可用");
        }
    }



    /**
     * 修改自己回答过的答案
     *
     * @param context               上下文
     * @param change_answer_content 修改后的内容
     * @param answer_user_objectId  答案ID
     * @param changeAnswerListener  修改listener
     */
    public void changeAnswer(Context context, String change_answer_content, String answer_user_objectId, final ChangeAnswerListener changeAnswerListener) {

        AnswerBean answer = new AnswerBean();
        answer.setAnswer_content(change_answer_content);
        answer.update(context, answer_user_objectId, new UpdateListener() {
            @Override
            public void onSuccess() {
                changeAnswerListener.ChangeAnswerOnSuccess();
            }

            @Override
            public void onFailure(int i, String s) {
                changeAnswerListener.ChangeAnswerOnFailure(s);
            }
        });
    }

    /**
     * 查找个人的所有答案
     *
     * @param context                  上下文
     * @param userBean                 用户
     * @param findAllMyAnswersListener 监听器
     */
    public void findAllMyAnswers(Context context, UserBean userBean, final FindAllMyAnswersListener findAllMyAnswersListener) {
        if (Tools.isNetAvailable(context)) {
            BmobQuery<AnswerBean> answers = new BmobQuery<>();
            answers.addWhereEqualTo("answerer", new BmobPointer(userBean));
            answers.order("-createdAt");
            answers.include("answer_of_question");
            answers.findObjects(context, new FindListener<AnswerBean>() {
                @Override
                public void onSuccess(List<AnswerBean> list) {
                    findAllMyAnswersListener.findMyAnswersOnSuccess(list, list.size());
                }

                @Override
                public void onError(int i, String s) {
                    findAllMyAnswersListener.findMyAnswersOnFailure(s);
                }
            });
        } else {
            findAllMyAnswersListener.findMyAnswersOnFailure("网络不可用");
        }
    }

    public void allMyAnswersCount(Context context, UserBean userBean, final FindAllMyAnswersListener findAllMyAnswersListener) {
        if (Tools.isNetAvailable(context)) {
            BmobQuery<AnswerBean> answers = new BmobQuery<>();
            answers.addWhereEqualTo("answerer", new BmobPointer(userBean));
            answers.order("-createdAt");
            answers.include("answer_of_question");
            answers.count(context, AnswerBean.class, new CountListener() {
                @Override
                public void onSuccess(int i) {
                    findAllMyAnswersListener.findMyAnswersOnSuccess(null, i);
                }

                @Override
                public void onFailure(int i, String s) {
                    findAllMyAnswersListener.findMyAnswersOnFailure(s);
                }
            });
        } else {
            findAllMyAnswersListener.findMyAnswersOnFailure("网络不可用");
        }
    }


    public void likeNumberChange(Context context, String answer_object, boolean isLike, final LikeNumberChangeListener likeNumberChangeListener) {
        if (Tools.isNetAvailable(context)) {
            AnswerBean answerBean = new AnswerBean();
            answerBean.setObjectId(answer_object);
            if (!isLike) {
                //如果之前不是关注的，就关注，关注数增加1
                answerBean.increment("answer_likes");
            } else {
                //如果之前是关注的，就取关，关注数减1
                answerBean.increment("answer_likes", -1);
            }
            answerBean.update(context, new UpdateListener() {
                @Override
                public void onSuccess() {
                    likeNumberChangeListener.likeNumberChangeOnSuccess();
                }

                @Override
                public void onFailure(int i, String s) {
                    likeNumberChangeListener.likeNumberChangeOnFailure(s);
                }
            });
        } else {
            likeNumberChangeListener.likeNumberChangeOnFailure("网络不可用");
        }
    }

    /**
     * 获取最新的答案状态
     *
     * @param context                context
     * @param answer_objectID        ID of the answer
     * @param checkTheAnswerListener listener
     */
    public void checkTheAnswer(Context context, String answer_objectID, final CheckTheAnswerListener checkTheAnswerListener) {
        if (Tools.isNetAvailable(context)) {
            BmobQuery<AnswerBean> answerBean = new BmobQuery<>();
            answerBean.include("answerer,answer_of_question");
            answerBean.getObject(context, answer_objectID, new GetListener<AnswerBean>() {
                @Override
                public void onSuccess(AnswerBean answerBean) {
                    checkTheAnswerListener.checkTheAnswerOnSuccess(answerBean);
                }

                @Override
                public void onFailure(int i, String s) {
                    checkTheAnswerListener.checkTheAnswerOnFailure(s);
                }
            });
        } else {
            checkTheAnswerListener.checkTheAnswerOnFailure("网络不可用");
        }
    }

    public void loadMoreAnswers(Context context, String earliestAnswer_createdAt, QuestionBean questionBean, final LoadMoreAnswersListener loadMoreAnswersListener) {
        if (Tools.isNetAvailable(context)) {

            BmobQuery<AnswerBean> answers = new BmobQuery<>();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = sdf.parse(earliestAnswer_createdAt);
            } catch (Exception e) {

            }
            answers.addWhereEqualTo("answer_of_question", new BmobPointer(questionBean));
            answers.addWhereGreaterThan("createdAt", new BmobDate(date));
            answers.setLimit(10);
            answers.include("answerer,answer_of_question");
            answers.findObjects(context, new FindListener<AnswerBean>() {
                @Override
                public void onSuccess(List<AnswerBean> list) {
                    loadMoreAnswersListener.loadMoreAnswersOnSuccess(list);
                }

                @Override
                public void onError(int i, String s) {
                    loadMoreAnswersListener.loadMoreAnswersOnFailure(s);
                }
            });

        } else {
            loadMoreAnswersListener.loadMoreAnswersOnFailure("网络不可用");
        }

    }

}
